package com.tedu.day07springmvc.controller;


import com.tedu.day07springmvc.mapper.UserMapper;
import com.tedu.day07springmvc.pojo.dto.UserDTO;
import com.tedu.day07springmvc.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private User user;

    // @RequestMapping 请求映射注解
//    @RequestMapping("/regUser")
//    @ResponseBody
//    public String regUser(HttpServletRequest request){
//        System.out.println("开始处理用户注册！！！");
//        String username =request.getParameter("username");
//        String password =request.getParameter("password");
//        String nickname =request.getParameter("nickname");
//        int age=Integer.parseInt(request.getParameter("age"));
//        System.out.println(username+","+password+","+nickname+","+age);
//        return "注册成功！";
//    }


    //  2. 获取请求参数 参数名与请求参数名一致
//    @RequestMapping("/regUser")
//    @ResponseBody
//    public String regUser(String username,String password,String nickname,int age){
//        System.out.println("注册信息"+username+","+password+","+nickname+","+age);
//        return "注册成功！";
//    }


    // 3. 获取请求参数 通过对象获取  设计数据传输对象  DTO
    @RequestMapping("/regUser")
    @ResponseBody
    public String regUser(UserDTO userDTO){
        System.out.println(userDTO);

        User user=new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setNickname(userDTO.getNickname());
        user.setAge(userDTO.getAge());
        user.setCreated(new Date());
        int num=userMapper.insertUser(user);
        return num>0?"注册成功":"注册失败";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(String username,String password){
        System.out.println(username+","+password);
        user=userMapper.selectUserByUsernameAndPassword(username,password);
        System.out.println(user);
        return user==null?"登录失败":"登录成功";
    }



}
